/**
 * -----------------------------------------------------------------
 * 单例模式
 * 需要文档规范，因为通过new的方法初始化SingalObject，也不会报错
 * 但是两个初始化的实例是不想等的，不符合单例的模式
 */

class SingalObject {
  login() {
    console.log('login...')
  }
}

// 定义静态的方法
SingalObject.getInstance = (function() {
  let intance;
  return function() {
    if (!intance) {
      intance = new SingalObject()
    }
    return intance
  }
})()

let obj1 = SingalObject.getInstance()
obj1.login()
let obj2 = SingalObject.getInstance()
obj2.login()
console.log('obj1 === obj2', obj1 === obj2)

console.log('------------------------------')
let obj3 = new SingalObject()   // 无法完全控制
obj3.login()
console.log('obj1 === obj3', obj1 === obj3)

/**
 * -----------------------------------------------------------------
 * 登录框
 */

class LoginForm {
  constructor() {
    this.state = 'hide'
  }
  show() {
    if (this.state === 'show') {
      alert('已经显示')
    }
    this.state = 'show'
    console.log('显示成功')
  }
  hide() {
    if (this.state === 'hide') {
      alert('已经关闭')
    }
    this.state = 'hide'
    console.log('关闭成功')
  }
}

LoginForm.getInstance = (function() {
  let instance;
  return function () {
    if (!instance) {
      instance = new LoginForm()
    }
    return instance
  }
})()

// 测试
let login1 = LoginForm.getInstance()
login1.show()

let login2 = LoginForm.getInstance()
login2.show()
